package com.shilox.android.app.logitnow.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.shilox.android.app.logitnow.R;
import com.shilox.android.app.logitnow.dao.LogMessageDAO;
import com.shilox.android.app.logitnow.data.LogMessage;
import com.shilox.android.app.logitnow.data.LogMessageAdapter;

public class ListFeelingsFragment extends Fragment implements OnClickListener {
	private static final int VERY_SAD_COLOR = Color.argb(255, 163, 139, 138);
	private static final int SAD_COLOR = Color.argb(255, 201, 211, 214);
	private static final int NORMAL_COLOR = Color.argb(255, 191, 230, 164);
	private static final int HAPPY_COLOR = Color.argb(255, 230, 140, 141);
	private static final int VERY_HAPPY_COLOR = Color.argb(255, 255, 54, 70);
	private static final String DAYS = "DAYS";

	private ListView mainListView;

	private LogMessageAdapter listAdapter;

	private int days;

	private LinearLayout botonera;

	private LinearLayout allLayout;
	private LinearLayout lastWeekLayout;
	private LinearLayout onlyTodayLayout;
	private LinearLayout lastMonthLayout;
	
	private View v;

	// DAO
	private LogMessageDAO dao;

	public static final ListFeelingsFragment newInstance(int daysParam) {
		ListFeelingsFragment f = new ListFeelingsFragment();

		Bundle bdl = new Bundle(1);
		bdl.putInt(DAYS, daysParam);
		f.setArguments(bdl);
		return f;
	}
	
	@Override
	public void onStart() {
		super.onStart();

		// Create DAO object
		dao = new LogMessageDAO(getActivity());
		// Set the list adapter and get TODOs list via DAO

		// Find the ListView resource.
		mainListView = (ListView) v.findViewById(R.id.list);

		setBotonera((LinearLayout) v.findViewById(R.id.botonera));

		onlyTodayLayout = (LinearLayout) v.findViewById(R.id.onlyTodayLayout);
		lastWeekLayout = (LinearLayout) v.findViewById(R.id.lastWeekLayout);
		lastMonthLayout = (LinearLayout) v.findViewById(R.id.lastMonthLayout);
		allLayout = (LinearLayout) v.findViewById(R.id.allLayout);

		listAdapter = new LogMessageAdapter(getActivity(), R.layout.row,
				dao.getLogMessages(null, null, null, 1));
		// Set the ArrayAdapter as the ListView's adapter.
		mainListView.setAdapter(listAdapter);

		registerForContextMenu(mainListView);

		onlyTodayLayout.setOnClickListener(this);
		lastWeekLayout.setOnClickListener(this);
		lastMonthLayout.setOnClickListener(this);
		allLayout.setOnClickListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.list_feelings, container, false);
		days = getArguments().getInt(DAYS);


		return v;
	}

	private void loadData(int days) {
		dao = new LogMessageDAO(getActivity());
		listAdapter.clear();
		for (LogMessage lm : dao.getLogMessages(null, null, null, days)) {
			listAdapter.add(lm);
		}
	}

	protected void deleteLogMessage(AdapterContextMenuInfo info) {

		// TODO item that was clicked
		LogMessage lm = (LogMessage) listAdapter.getItem((int) info.id);

		// Delete TODO object from the database
		dao.deleteLogMessage(lm.getId());

		// Set the list adapter and get TODOs list via DAO
		loadData(days);

		// Display success information
		Toast.makeText(getActivity().getApplicationContext(), "Log deleted!",
				Toast.LENGTH_LONG).show();
	}

	@Override
	public void registerForContextMenu(View view) {
		// TODO Auto-generated method stub
		super.registerForContextMenu(view);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.share:
			LogMessage lm = (LogMessage) listAdapter.getItem((int) info.id);
			Intent sendIntent = new Intent();
			sendIntent.setAction(Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT, lm.getTexto());
			sendIntent.setType("text/plain");
			startActivity(Intent.createChooser(sendIntent, getResources()
					.getText(R.string.send_to)));
			return true;
		case R.id.delete:
			deleteLogMessage(info);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.allLayout:
			// Display success information
			days = 0;
			break;
		case R.id.lastMonthLayout:
			// Display success information
			days = 30;
			break;
		case R.id.lastWeekLayout:
			// Display success information
			days = 7;
			break;
		case R.id.onlyTodayLayout:
			// Display success information
			days = 1;
			break;
		}

		loadData(days);
		mainListView.setVisibility(View.VISIBLE);
		getBotonera().setVisibility(View.GONE);
	}
	
	public void showBotonera() {
		getBotonera().setVisibility(View.VISIBLE);
		mainListView.setVisibility(View.GONE);
	}

	public LinearLayout getBotonera() {
		return botonera;
	}

	public void setBotonera(LinearLayout botonera) {
		this.botonera = botonera;
	}

}
